package com.lwl.blue.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lwl.blue.commont.base.*;
import com.lwl.blue.commont.utils.UserContextUtil;
import com.lwl.blue.entity.PurchaseOrderDetail;
import com.lwl.blue.service.PurchaseOrderDetailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/purchaseOrderDetail")
public class PurchaseOrderDetailController extends BaseController {

    @Autowired
    private PurchaseOrderDetailService purchaseOrderDetailService;

    /**
     * 分页查询采购订单明细。
     */
    @PostMapping("/pages")
    public PageResult<PurchaseOrderDetail> pages(@RequestBody PageParam<PurchaseOrderDetail> pageParam) {
        JsonResult<Page<PurchaseOrderDetail>> pages = purchaseOrderDetailService.pages(pageParam);
        PageResult<PurchaseOrderDetail> re = new PageResult<>();
        re.success(pages.getData().getTotal(), pages.getData().getRecords());
        return re;
    }

    /**
     * 分页查询采购订单明细（根据采购订单ID）。
     */
    @PostMapping("/pagesDetail")
    public PageResult<PurchaseOrderDetail> pagesDetail(@RequestBody PageParam<PurchaseOrderDetail> pageParam) {
        PageResult<PurchaseOrderDetail> re = new PageResult<>();
        if (null == pageParam.getParam() || null == pageParam.getParam().getPurchaseOrderId()) {
            re.success(0L, new java.util.ArrayList<>());
            return re;
        }
        JsonResult<Page<PurchaseOrderDetail>> pages = purchaseOrderDetailService.pages(pageParam);
        re.success(pages.getData().getTotal(), pages.getData().getRecords());
        return re;
    }

    /**
     * 新增或更新采购订单明细（根据是否存在主键区分）。
     */
    @PostMapping("/saveOrUpdate")
    public JsonResult<String> saveOrUpdate(@RequestBody PurchaseOrderDetail param) {
        setInsetOrUpdateUser(param);
        // 兼容前端：编辑页传的是 purchaseOrderDetailId，将其映射为 id，避免被当做新增
        if (param.getId() == null && param.getPurchaseOrderDetailId() != null) {
            param.setId(param.getPurchaseOrderDetailId());
        }
        purchaseOrderDetailService.saveOrUpdate(param);
        return new JsonResult<String>().success("请求成功", null);
    }

    /**
     * 物理删除采购订单明细（直接从数据库删除）。
     */
    @PostMapping("/delete/{id}")
    public JsonResult<String> delete(@PathVariable("id") Long id) {
        purchaseOrderDetailService.removeById(id);
        return new JsonResult<String>().success("请求成功", null);
    }

    /**
     * 批量物理删除采购订单明细（直接从数据库删除）。
     */
    @PostMapping("/deleteByIds")
    public JsonResult<String> deleteByIds(@RequestBody List<Long> ids) {
        purchaseOrderDetailService.removeByIds(ids);
        return new JsonResult<String>().success("请求成功", null);
    }

    /**
     * 根据ID查询单条采购订单明细。
     */
    @PostMapping("/getById/{id}")
    public JsonResult<PurchaseOrderDetail> getById(@PathVariable("id") Long id) {
        return purchaseOrderDetailService.getByPk(id);
    }
}
